<?php

class TagDocumentController extends \BaseController {


	public function showScanDocument()
	{
		return View::make('scanDocument');
	}   
		
	public function showScanDocumentS2()
	{
		$user = Auth::User();
		$upload_dir = public_path().'/uploads/'.$user->username.'/';
		$files = File::files($upload_dir);
		$templates = $user->department->templates;
		if(count($templates) > 0){
			$template_options = array_combine($templates->lists('id'), $templates->lists('template_name'));
		} else {
			$template_options = array(null, 'Unspecified');
		}
		return View::make('scanDocumentS2')->with(array('files' => $files, 'template_options' => $template_options));
	}
	
	public function showScanDocumentS3()
	{
		$template_id = Input::get('template_id');
		$user = Auth::User();
		$upload_dir = public_path().'/uploads/'.$user->username.'/';
		$files = File::files($upload_dir);
		$template = Template::find($template_id);
		
		
		return View::make('scanDocumentS3')->with(array('files' => $files, 'template' => $template));
	}
	
	public function saveScanDocument($template_id)
	{
		$user = Auth::User();
		$template = Template::find($template_id);
		$upload_dir = public_path().'/uploads/'.$user->username.'/';
		$files = File::files($upload_dir);
		
		$output_file = TagDocumentController::convertJPG2TIFF($user);
				
		if (File::exists($output_file)) {
		
			$document = new Document;
			$document->create_by = $user->username;
			$document->update_by = $user->username;
			$document->template_id = $template->id;
			$document->content = File::get($output_file);
			$document->save();
			
			foreach ($template->templateParams as $templateParam)
			{
				$template_param_id = $templateParam->id;
				$documentIndex = new DocumentIndex;
				$documentIndex->document_id = $document->id;
				$documentIndex->template_param_id = $templateParam->id;
				$documentIndex->index_value = Input::get('template_param_'.$template_param_id);
				
				//$documentIndex->save();
				$document->documentIndex()->save($documentIndex);
			}
			
			TagDocumentController::deleteAllFile($upload_dir);
			
			return Redirect::to('home')->with('success', 'Document successfully added');
		} else {
			return View::make('scanDocumentS3')->withErrors('UnSuccess', 'Document Unsuccessfully added')
						->with(array('files' => $files, 'template' => $template));
		}
		
		
	
	}
	
	private function convertJPG2TIFF($user)
	{
		$upload_dir = public_path().'/uploads/'.$user->username.'/';
		$files = File::files($upload_dir);
		
		$listOfFileName = '';
		foreach($files as $file) {
			$file_name = File::name($file);
			$file_ext = File::extension($file);
			$jpg_file_name = $file_name . '.' . $file_ext;
			
			$listOfFileName = $listOfFileName . ' '. $upload_dir . $jpg_file_name;
		}
		
		$output_file = public_path().'/uploads/' . $user->username. '/temp.tiff';
		$exec_cmd = 'convert -compress lzw -depth 8 ' . $listOfFileName . ' ' . $output_file;
		// Convert jpg to tiff
		Log::info($exec_cmd);
		exec($exec_cmd);
		
		return $output_file;
	}

	public function deleteAllFile($img_path) {
		$files = File::files($img_path);
		foreach($files as $file_path) {
			File::delete($file_path);
		}
	}
}
